import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 测试类
 */
public class Test {

    /**
     * 按照名字查记录
     */
    private static void selectByName() {
        User user = new User();
        user.setUserName("朱竹清");
        Connection con = null;
        PreparedStatement pstmt = null;
        JDBCUtil jdbc = new JDBCUtil();
        UserDao userDao = new UserDao();
        ResultSet rs = null;
        try {
            con = jdbc.gitConn();
            if (con != null) {
                System.out.println("连接数据库成功！！！");
                rs = userDao.queryUser(con, pstmt, user.getUserName());
                while (rs.next()) {
                    System.out.print(rs.getInt("id") + " ");
                    System.out.print(rs.getString("username") + " ");
                    System.out.print(rs.getString("password") + " ");
                    System.out.print(rs.getInt("age"));
                    System.out.println();
                }
            } else {
                System.out.println("连接数据库失败！！！");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                rs = null;
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            try {
                jdbc.closeConn(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 查询数据库表中的所有记录
     */
    private static void selectAll() {
        JDBCUtil jdbc = new JDBCUtil();
        Connection con = null;
        PreparedStatement pstmt = null;
        UserDao userDao = new UserDao();
        ResultSet rs = null;
        // 建立数据库链接
        try {
            con = jdbc.gitConn();
            rs = userDao.queryUserAll(con, pstmt);
            //解析rs结果集
            //jdbc.rs2List(rs);
            //遍历list集合
            jdbc.printList(jdbc.rs2List(rs));

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                rs = null;
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            try {
                jdbc.closeConn(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        selectByName();
        selectAll();
    }


}
